﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CTemplateColumn : ITemplate
    {
        private string colname;
        private TCType tcType;

        public CTemplateColumn(string cname, TCType colType)
        {
            this.colname = cname;
            this.tcType = colType;
        }

        public void InstantiateIn(Control container)
        {
            Control control;
            if (this.tcType == TCType.TextBox)
            {
                control = new TextBox();
                control.ID = "tb1";
            }
            else
            {
                control = new LiteralControl();
            }
            control.DataBinding += new EventHandler(this.OnDataBinding);
            container.Controls.Add(control);
        }

        public void OnDataBinding(object sender, EventArgs e)
        {
            if (this.tcType == TCType.TextBox)
            {
                TextBox box = (TextBox) sender;
                GridViewRow namingContainer = (GridViewRow) box.NamingContainer;
                box.Text = ((DataRowView) namingContainer.DataItem).Row[this.colname].ToString();
            }
            else
            {
                LiteralControl control = (LiteralControl) sender;
                GridViewRow row2 = (GridViewRow) control.NamingContainer;
                control.Text = ((DataRowView) row2.DataItem).Row[this.colname].ToString();
            }
        }
    }
}

